<template>
  <div>
    <el-form
      :model="passForm"
      ref="passForm"
      :rules="rules"
      label-width="95px"
      size="small"
    >
      <el-form-item label="旧密码" prop="oldpd">
        <el-input
          type="password"
          v-model="passForm.oldpd"
          show-password
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newpd">
        <el-input
          type="password"
          v-model="passForm.newpd"
          show-password
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="cnewpd">
        <el-input
          type="password"
          v-model="passForm.cnewpd"
          show-password
          clearable
        ></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { modifyPwd } from "@/service/user";
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (!value) return;
      if (value === this.passForm.oldpd) {
        callback(new Error("新密码不能与旧密码相同!"));
      } else {
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (!value) return;
      if (value !== this.passForm.newpd) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      isClose: false,
      passForm: {
        oldpd: "",
        newpd: "",
        cnewpd: "",
      },
      rules: {
        oldpd: [{ required: true, message: "请输入旧密码", trigger: "blur" }],
        newpd: [
          { required: true, message: "请输入新密码", trigger: "blur" },
          { validator: validatePass, trigger: "blur" },
        ],
        cnewpd: [
          { required: true, message: "确认新密码", trigger: "blur" },
          { validator: validatePass2, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    modifypw() {
      this.$refs["passForm"].validate((valid) => {
        if (valid) {
          modifyPwd({
            oldpwd: this.passForm.oldpd,
            newpwd: this.passForm.newpd,
          }).then((res) => {
            if (res.succeed) {
              this.$message({
                type: "success",
                message: "成功!",
              });
              this.isClose = true;
            } else {
              this.$message({
                type: "error",
                message: res.errMsg,
              });
            }
          });
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style>
</style>